package com.icebergteam.warfacewidget.core;

import java.util.Timer;
import java.util.TimerTask;
import android.appwidget.AppWidgetManager;
import android.content.ComponentName;
import android.content.Intent;
import android.net.Uri;

import com.icebergteam.warfacewidget.AppManager;
import com.icebergteam.warfacewidget.WidgetProvider;
import com.icebergteam.warfacewidget.core.settings.NewsSettings;

/**
 * @author Dima
 *
 */
public class MainTimer {
	private static Timer timer = null;
	public static final String className = MainTimer.class.getCanonicalName(); 
	public static final int PERIOD = 1000 * 60 * 60;
	
	public static void runTimer() {
		if (timer == null) {
		
			    
			timer = new Timer();
			timer.schedule(new TimerTask() {
				
				@Override
				public void run() {
					try {
						if(!AppManager.getNewsSettings().isRSSAutoupdate()){
							return;
						}
						NewsSettings resSet = AppManager.getNewsSettings();
						long resCurrentTimestamp = resSet.getRSSLastUpdateTimestamp();
							
						int tick = resSet.getRSSUpdatePeriod() * MainTimer.PERIOD;
						
						long currentTimestamp = new java.sql.Timestamp(new java.util.Date().getTime()).getTime();
						if(resCurrentTimestamp == 0){
							resSet.setRSSLastUpdateTimestamp(currentTimestamp);
						}
							
						if(resCurrentTimestamp != 0 && currentTimestamp - resCurrentTimestamp > tick){
							MainTimer.sendBroadcastUpdate(AppManager.getNewsSettings().getRSSUpdatePeriod() * MainTimer.PERIOD);
							
						}
					} catch (Exception e) {
						LogManager.LogE(MainTimer.className,  LogManager.getExecuteMethodName(),e.getLocalizedMessage());
						e.printStackTrace();
					}
				}
			}, 0L, 1);
		}


	}
	/**
	 * @param tick
	 */
	private static void sendBroadcastUpdate(int tick) {
		
			LogManager.LogV(MainTimer.className, LogManager.getExecuteMethodName(), tick, AppManager.getNewsSettings().isRSSAutoupdate());
			final AppWidgetManager widgetManager = AppWidgetManager
					.getInstance(AppManager.getContext());
			final ComponentName widgetName = new ComponentName(
					AppManager.getContext(), WidgetProvider.class);

			final int[] widgetIds = widgetManager
					.getAppWidgetIds(widgetName);

			for (int index : widgetIds) {
				final Intent refreshIntent = new Intent(
						AppManager.getContext(), WidgetProvider.class);
				refreshIntent
						.setAction(WidgetProvider.UPDATE_ACTION);
				refreshIntent.putExtra(
						AppWidgetManager.EXTRA_APPWIDGET_ID,
						index);
				refreshIntent.putExtra(WidgetProvider.TYPE_LIST, WidgetProvider.getTypeCurrentService());
				refreshIntent.setData(Uri.parse(refreshIntent
						.toUri(Intent.URI_INTENT_SCHEME)));
				AppManager.getContext().sendBroadcast(refreshIntent);
			}
			NewsSettings resSet = AppManager.getNewsSettings();
			resSet.setRSSLastUpdateTimestamp(new java.sql.Timestamp(new java.util.Date().getTime()).getTime());
			resSet.save();
		
	}
}
